package cn.com.bluemoon.daps.model.utils.factory.impl.table;

import cn.com.bluemoon.daps.model.dto.DapDataModelTableDto;
import cn.com.bluemoon.daps.model.dto.ModelTableInfoDto;
import cn.com.bluemoon.daps.model.dto.SubordinateKeyDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.entity.DapDataModelTablePartition;
import cn.com.bluemoon.daps.model.utils.SqlUtils;
import cn.com.bluemoon.daps.model.utils.contrast.Constant;
import cn.com.bluemoon.daps.model.utils.factory.TableScriptFactory;
import cn.com.bluemoon.daps.model.utils.factory.impl.field.HiveFieldScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.partition.HivePartitionScript;
import com.alibaba.druid.DbType;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author karry
 * @Data 2021/9/3
 * @Time:14:08
 */
public class HiveTableScript extends AbsTableScript {

    public final static DbType HIVE = DbType.hive;

    public HiveTableScript() {
        super(HIVE);
    }

    @Override
    public String createTableScript(ModelTableInfoDto dto, List<SubordinateKeyDto> keys, List<DapDataModelTablePartition> partitions) {

        String schema = StringUtils.isNotBlank(dto.getTableSchema()) ? dto.getTableSchema()+"." : "";
        String fieldScript = dto.getFieldList().isEmpty() ? "" : hiveFieldScript.createFieldScript(dto.getFieldList());
        String partitionScript = partitions.isEmpty() ? "" : hivePartitionScript.createPartitionScript(partitions);
        String storesAs = String.format("STORED AS %s", SqlUtils.getStoredAs(dto.getHiveStoredAs()));
        String properties = StringUtils.isBlank(dto.getHiveSerdeProps()) ? "" : String.format("WITH SERDEPROPERTIES (%s)", dto.getHiveSerdeProps());
        String location = "";// StringUtils.isBlank(dto.getHiveLocation()) ? "" : String.format("LOCATION '%s'", dto.getHiveLocation());
        String tableProperties = StringUtils.isBlank(dto.getHiveTableProps()) ? "" : String.format("TBLPROPERTIES (%s)", dto.getHiveTableProps());
        String format = String.format("%s %s %s %s", storesAs, properties, location, tableProperties);
        String tableScript = String.format("create table %s`%s`(%s) %s %s;", schema, dto.getEngName(), fieldScript, partitionScript, format);
        return tableScript;
    }

    @Override
    public void modifyTableScript(DapDataModelTableDto newDto, DapDataModelTableDto oldDto,
                                  String tableSchema, Map<String, String> map) {
        String schema = StringUtils.isNotBlank(newDto.getTableSchema()) ? newDto.getTableSchema()+"." : "";
        // 查询英文表名是否发生修改，表中文名不考虑
        if (!StringUtils.equalsIgnoreCase(newDto.getEngName(), oldDto.getEngName())) {
            map.put(Constant.MT, String.format("%s%s%s rename to %s;", Constant.AT, schema, oldDto.getEngName(), newDto.getEngName()));
        }
    }

    @Override
    public void deleteTableScript(String tableName, String tableSchema, Map<String, String> map) {

        String schema = StringUtils.isNotBlank(tableSchema) ? tableSchema + "." : "";
        String dropScript = String.format("%s%s%s;", Constant.DTA, schema, tableName);
        map.put(Constant.DT, dropScript);
    }

}
